﻿/**********************************************
 * 功能名称：操作员管理
 *列表显示操作员，可对操作员信息进行增删改。
 * 
 * 
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Management
{
    public partial class UserManagement : CSMPageBase
    {
        CustomDataContext db = new CustomDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

              
                    BindMyData(0, "");
                    int Totals = TotalPageAmount("");
                    int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                    Label_Total.Text = "共" + pages.ToString();
                    Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";
                    EditFlag.Value = "add";
                
            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex,string starts)
        {
            int skipAccount = PageIndex * 10;
            var MyQuery = (from a in db.TCsm_JS_User
                           where a.UserName.StartsWith(starts)
                           select new
                           {
                               a.UserID,
                               a.UserName,
                               a.IsStop,
                               a.CreateTime,
                               a.ModifyTime
                           }).Skip(skipAccount).Take(10);
            GridView_Users.DataSource = MyQuery;
            GridView_Users.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount(string starts)
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TCsm_JS_User where a.UserName.StartsWith(starts) select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            string starts = txtUser.Text;
            int Totals = TotalPageAmount(starts);



            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx,starts);

        }

        #endregion
        //取消的按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            EditFlag.Value = "0";
            Panel_Edit.Visible = false;
            Panel_Gridlist.Visible = true;
            if (EditFlag.Value == "edit")
            {
                TextBox_UserId.Enabled = true;

            }

        }

        //增加按钮的事件
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_Gridlist.Visible = false;
            TextBox_UserId.Text = "";
            TextBox_UserName.Text = "";
            CheckBox_StopFlag.Checked = false;
            Panel_Edit.Visible = true;
            TextBox_UserId.Enabled = true;
            EditFlag.Value = "add";
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('增加','Panel_Edit');</script>", false);
        }

        //保存的按钮
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {

            if (EditFlag.Value == "add")
            {


                TCsm_JS_User myrow = new TCsm_JS_User();
                myrow.UserID = TextBox_UserId.Text;
                myrow.UserName = TextBox_UserName.Text;
                myrow.IsStop = CheckBox_StopFlag.Checked;
                myrow.CreateTime = DateTime.Now;
                myrow.ModityID = Session[SystemVars.sysSessionUserId].ToString();
                myrow.UserRightID = "";
                myrow.RoleID = "";
                myrow.Password = FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5");
                db.TCsm_JS_User.InsertOnSubmit(myrow);


                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");

                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    BindMyData(0,"");
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }


            }
            else if (EditFlag.Value == "edit")
            {
                string myUserId = TextBox_UserId.Text;
                var myEditQuery = (from a in db.TCsm_JS_User where a.UserID == myUserId select a).Single();

                myEditQuery.UserName = TextBox_UserName.Text;
                myEditQuery.IsStop = CheckBox_StopFlag.Checked;
                myEditQuery.ModityID = Session[SystemVars.sysSessionUserId].ToString();
                myEditQuery.ModifyTime = DateTime.Now;

                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑成功'); </script>");

                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    TextBox_UserId.Enabled = true;
                    BindMyData(0,"");

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑失败');</script>");
                }

            }

        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            var myEditQuery = (from a in db.TCsm_JS_User
                               where a.UserID == myid
                               select new
                               {
                                   a.UserID,
                                   a.UserName,
                                   a.IsStop
                               }
                               ).Single();
            TextBox_UserId.Text = myEditQuery.UserID;
            TextBox_UserName.Text = myEditQuery.UserName;
            CheckBox_StopFlag.Checked = myEditQuery.IsStop;
            TextBox_UserId.Enabled = false;
            EditFlag.Value = "edit";
            Panel_Edit.Visible = true;
            Panel_Gridlist.Visible = false;
            ScriptManager.RegisterStartupScript((System.Web.UI.Page)HttpContext.Current.CurrentHandler, typeof(System.Web.UI.Page), "addtext", "<script>ShowThickBox('编辑','Panel_Edit');</script>", false);
        }

        protected void Button_Browse_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text;
            BindMyData(0, user);
        }


    }
}
